//
//  CTCommentCell.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/1.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTCommentCell.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "UIViewAdditions.h"
#import "CTRequestUtil.h"
#import "CTTextManager.h"
#import "CTLogicItems.h"

@interface CTCommentCell()
{
    UIView              *backGroundView;
    UIImageView         *headImageView;
    UILabel             *nameLabel;
    UILabel             *timeLabel;
    UILabel             *commentLabel;
    UILabel             *bottomLine;
}

@end

@implementation CTCommentCell
@synthesize delegate;
- (void)dealloc
{
    [bottomLine removeFromSuperview];
    bottomLine = nil;
    [commentLabel removeFromSuperview];
    commentLabel = nil;
    [timeLabel removeFromSuperview];
    timeLabel = nil;
    [nameLabel removeFromSuperview];
    nameLabel = nil;
    [headImageView removeFromSuperview];
    headImageView = nil;
    [backGroundView removeFromSuperview];
    backGroundView =nil;
    
    [CTDebugManager showErrorLogInfo:@"dealloc : CTCommentCell Is Removed!!!!"];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
//        self.favourBool = YES;
//        favNumber = 0;
        [self awakeFromNib];
    }
    return self;
}

- (void)awakeFromNib
{
    backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 79)];
    [self addSubview:backGroundView];
    //头像
    headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 38, 38)];
    headImageView.layer.masksToBounds = YES;
    headImageView.userInteractionEnabled = YES;
    headImageView.layer.cornerRadius  = 19.0f;
    headImageView.image = [UIImage imageNamed:@"head"];
    [backGroundView addSubview:headImageView];
//    UITapGestureRecognizer *tapHeadGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHeadGestureAction:)];
//    [headImageView addGestureRecognizer:tapHeadGesture];
    //名字
    nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(headImageView.right+10, 20, 200, 15)];
    nameLabel.font = [UIFont systemFontOfSize:15];
    nameLabel.text = @"李二麻子";
    nameLabel.textColor = CT_HEADER_BLACK_COLOR;
    nameLabel.textAlignment = NSTextAlignmentLeft;
    [backGroundView addSubview:nameLabel];
    //时间
    timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-120, 23, 110, 15)];
    timeLabel.font = [UIFont systemFontOfSize:12];
    timeLabel.text = @"5分钟前";
    timeLabel.textColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
    timeLabel.textAlignment = NSTextAlignmentRight;
    [backGroundView addSubview:timeLabel];
    //评论
    commentLabel = [[UILabel alloc]initWithFrame:CGRectMake(headImageView.right+10, nameLabel.bottom+10, CT_HEADER_SCREEN_WIDTH-58-40, 15)];
    commentLabel.font = [UIFont systemFontOfSize:15];
    commentLabel.text = @"5分钟前";
    commentLabel.numberOfLines =0;
//    commentLabel.backgroundColor = [UIColor redColor];
    commentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    commentLabel.textColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
    commentLabel.textAlignment = NSTextAlignmentLeft;
    [backGroundView addSubview:commentLabel];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)createBottomLine
{
    [bottomLine removeFromSuperview];
    bottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, backGroundView.height-0.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    bottomLine.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [backGroundView addSubview:bottomLine];
}
- (float)LabelHeightLabel:(UILabel*)label fontSize:(float)fontSize
{
    
    CGSize  titleSize = [label.text sizeWithFont:[UIFont systemFontOfSize:fontSize]
                               constrainedToSize:CGSizeMake(CT_HEADER_SCREEN_WIDTH-58-40, 2000)
                                   lineBreakMode:NSLineBreakByCharWrapping];
    label.frame = CGRectMake(label.frame.origin.x,
                             label.frame.origin.y,
                             label.frame.size.width,
                             titleSize.height);
    return titleSize.height;
}

#pragma  mark -setter-
- (void) setNameLabel:(NSString *)nameStr
{
    nameLabel.text = nameStr;
}
- (void) setTimeLabel:(NSString *)timeStr
{
    CTTextManager *textManager = [CTTextManager shareInstance];
    timeLabel.text = [textManager timestampToTime:timeStr ];
}
- (void) setCommentLabel:(NSString *)commentStr
{
    commentLabel.text = commentStr;

    commentLabel.frame  = CGRectMake(headImageView.right+10, nameLabel.bottom+10, CT_HEADER_SCREEN_WIDTH-58-40, [self LabelHeightLabel:commentLabel fontSize:15]);
    backGroundView.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 65+commentLabel.height);
    [self createBottomLine];
}
- (void) setHeadImage:(NSString *)imgUrl
{
   
    [CTRequestUtil requestImgWithUrl:imgUrl withCompletionSuccessHandler:^(UIImage *img) {
        headImageView.image = img;
    } withFailedHandler:^(NSError *error) {
        
    }];
    
}
//- (void)tapHeadGestureAction:(UITapGestureRecognizer *) sender
//{
//    if ([self.delegate respondsToSelector:@selector(clickHeadBtnAction:withItem:)])
//    {
//        [self.delegate clickHeadBtnAction:self withItem:self.commentListItem];
//    }
//}
@end
